from django.contrib.auth.backends import ModelBackend
from rest_framework.mixins import RetrieveModelMixin,ListModelMixin
from django.http import HttpRequest
import re
from users.models import User
# from meiduo_mall.apps.users.models import User


class MeiduoModelBackend(ModelBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):
        #
        if request is None:
            try:
                admin = User.objects.get(username=username, is_staff=True)
            except:
                # 如果未查到数据，则返回None，用于后续判断
                admin = None
                    # return None
            # 判断密码
            if admin is not None and admin.check_password(password):
                return admin
        else:
            # 前台
            try:
                user = User.objects.get(username=username)
            except:
                try:
                    user = User.objects.get(mobile=username)
                except:
                    return None
            if user .check_password(password):
                return user
            else:
                return None

